// https://umijs.org/config/
import os from 'os';
// eslint-disable-next-line import/no-extraneous-dependencies
import slash from 'slash2';
import pageRoutes from './router.config';
import webpackPlugin from './plugin.config';
import defaultSettings from '../src/defaultSettings';
const { pwa } = defaultSettings;

// 代理地址：
// eolinker :
// const proxyUrl = 'http://10.0.75.47:54321/eolinker_os/Mock/simple?projectID=1&uri=/';
// 开发 :http://10.0.75.48
// 测试 :http://10.0.75.38
// uat :http://10.0.75.54
const proxyUrl = process.env.PROXY_URL;

const plugins = [
  [
    'umi-plugin-react',
    {
      antd: true,
      dva: {
        immer: false,
        hmr: true,
        dynamicImport: undefined,
      },
      locale: {
        enable: true, // default false
        default: 'zh-CN', // default zh-CN
        baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
      },
      dynamicImport: {
        loadingComponent: './components/PageLoading/index',
        webpackChunkName: true,
      },
      pwa: pwa
        ? {
            workboxPluginMode: 'InjectManifest',
            workboxOptions: {
              importWorkboxFrom: 'local',
            },
          }
        : false,
      ...(!process.env.TEST && os.platform() === 'darwin'
        ? {
            dll: {
              include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
              exclude: ['@babel/runtime'],
            },
            hardSource: false,
          }
        : {}),
    },
  ],
  [
    'umi-plugin-pro-block',
    {
      moveMock: false,
      moveService: false,
      modifyRequest: true,
      autoAddMenu: true,
    },
  ],
];

export default {
  // add for transfer to umi
  plugins,
  // block: {
  //   defaultGitUrl: 'https://github.com/ant-design/pro-blocks',
  // },
  hash: true,
  define: {
    APP_TYPE: process.env.APP_TYPE || '',
    'process.env.PROXY_URL': process.env.PROXY_URL || '',
  },
  treeShaking: true,
  targets: {
    ie: 11,
  },
  // 路由配置
  routes: pageRoutes,
  // Theme for antd
  // https://ant.design/docs/react/customize-theme-cn
  theme: {
    'primary-color': defaultSettings.primaryColor,
  },
  externals: {
    '@antv/data-set': 'DataSet',
  },
  proxy: {
    '/ssoApi/': {
      target: proxyUrl,
      changeOrigin: true,
      // pathRewrite: { '^/telematics': '' },
    },
    '/nsApi/': {
      target: proxyUrl,
      changeOrigin: true,
      // pathRewrite: { '^/telematics': '' },
    },
    '/esApi/': {
      target: proxyUrl,
      changeOrigin: true,
      // pathRewrite: { '^/telematics': '' },
    },
    '/csApi/': {
      target: proxyUrl,
      changeOrigin: true,
      // pathRewrite: { '^/telematics': '' },
    },
    '/osApi/': {
      target: proxyUrl,
      changeOrigin: true,
      // pathRewrite: { '^/telematics': '' },
    },
    '/mgrApi/': {
      target: proxyUrl,
      changeOrigin: true,
      // pathRewrite: { '^/telematics': '' },
    },
  },
  ignoreMomentLocale: true,
  lessLoaderOptions: {
    javascriptEnabled: true,
  },
  disableRedirectHoist: true,
  cssLoaderOptions: {
    modules: true,
    getLocalIdent: (context, localIdentName, localName) => {
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }
      const match = context.resourcePath.match(/src(.*)/);
      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
          .map(a => a.replace(/([A-Z])/g, '-$1'))
          .map(a => a.toLowerCase());
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }
      return localName;
    },
  },
  manifest: {
    basePath: '/',
  },
  devtool: devtool(),
  chainWebpack: webpackPlugin,
};

function devtool() {
  if (process.env.NODE_ENV === 'production') {
    if (process.env.SOURCE_MAP === 'true') {
      return 'source-map';
    } else {
      return false;
    }
  } else {
    return 'cheap-module-source-map';
  }
}
